package com.qqclient.serivce;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class MessageClientService {
    /**
     *
     * @param contents 消息内容
     * @param senderId 发送者Id
     */
    public void sendMessageToAll(String contents, String senderId){
        //构建message
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(contents);
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);//消息类型
        message.setSendTime(new Date().toString());//发送时间设置到message对象
        System.out.println(senderId+"向在线用户发送消息");
        //发送消息
        try {
            ObjectOutputStream oos = new ObjectOutputStream
                    (ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @param content 消息内容
     * @param senderId 发送者Id
     * @param getterId 接收者Id
     */
    public void sendMessageToOne(String content,String senderId,String getterId){
        //构建message
        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_COMM_MES);//消息类型
        message.setSendTime(new Date().toString());//发送时间设置到message对象
        System.out.println(senderId+"向"+getterId+"发送消息");
        //发送消息
        try {
            ObjectOutputStream oos = new ObjectOutputStream
                    (ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
